package com.industrial.white.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.util.StringUtils;
import com.industrial.common.core.domain.R;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.system.api.RemoteUserService;
import com.industrial.system.api.domain.RegisterWhiteUserForm;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.domain.EmEnterpriseInformationCollection;
import com.industrial.white.domain.WhiteIndustrialArea;
import com.industrial.white.mapper.EmEnterpriseInformationCollectionMapper;
import com.industrial.white.mapper.WhiteIndustrialAreaMapper;
import com.industrial.white.service.ICommonService;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommonServiceImpl implements ICommonService {

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private WhiteIndustrialAreaMapper whiteIndustrialAreaMapper;

    @Autowired
    private EmEnterpriseInformationCollectionMapper emEnterpriseInformationCollectionMapper;

    @Override
    public List<WhiteIndustrialArea> getAdministrativeDistrictData(String adCode) {
        List<WhiteIndustrialArea> sysAreas = this.whiteIndustrialAreaMapper.queryAdministrativeDistrict(adCode);
        return sysAreas;
    }

    @Override
    public String loadAdCodeToAdBaseId(String adCode) {
        String value = whiteIndustrialAreaMapper.loadAdCodeToAdBaseId(adCode);
        return value;
    }

    /**
     * <p>
     * 注册白名单用户
     * </p>
     *
     * @param sysUser
     * @return
     */
    @Override
    public AjaxResult registerWithUser(RegisterWhiteUserForm params) {
        List<String> userNameList = new ArrayList<>();
        userNameList.add(params.getUsci());
        R<List<SysUser>> currentUserNameExist = remoteUserService.getCurrentUserNameExist(userNameList);
        if(currentUserNameExist.getCode() == R.SUCCESS){
            if(currentUserNameExist.getData().isEmpty()){
                R<Boolean> isRegisterSuccess = remoteUserService.registerWithUser(params);
                if(isRegisterSuccess.getCode() == R.SUCCESS){
                    insertBaseEnterInfo(params);
                    return AjaxResult.success("注册成功");
                }else{
                    return AjaxResult.warn("注册失败");
                }
            }else{
                return AjaxResult.warn("当前社会信息码已被注册");
            }
        }else{
            return AjaxResult.warn("用户信息查询失败");
        }
    }

    /**
     * <p>
     *     用户成功注册后通过此方法插入企业的基本信息
     * </p>
     * @param params
     */
    private void insertBaseEnterInfo(RegisterWhiteUserForm params) {
        //填入企业基本信息
        String usci = params.getUsci();
        String province = params.getProvince();
        String city = params.getCity();
        String county = params.getCounty();
        String enterName = params.getEnterName();

        EmEnterpriseInformationCollection p = new EmEnterpriseInformationCollection();
        p.setBaseId(IdUtil.simpleUUID());
        p.setEnterProvince(province);
        p.setEnterCity(city);
        p.setEnterCounty(county);
        p.setEnterName(enterName);
        p.setEnterUsci(usci);
        p.setEnterContactsPhone(params.getPhone());

        emEnterpriseInformationCollectionMapper.insertEmEnterpriseInformationCollection(p);
    }

    /**
     * 查询行政区代码所对应的行政区ID
     *
     * @param codes
     * @return
     */
    @Override
    public List<WhiteIndustrialArea> queryWhiteAreaId(String codes) {
        if(StringUtils.isNotBlank(codes)){
            String[] codeArgs = codes.split(",");
            List<WhiteIndustrialArea> r = whiteIndustrialAreaMapper.queryAreaIdByAreaCode(codeArgs);
            return r;
        }
        return null;
    }
}
